package com.jiyun.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jiyun.pojo.Comment;
import com.jiyun.pojo.JsonRes;
import com.jiyun.service.CommentService;
import com.jiyun.utils.AliyunOSSClientUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;

/**
 * @author: 刘清芳
 * @description:
 * @date: 2023/12/28 17:19
 */
@RestController
@RequestMapping("comment")
public class CommentController {
    @Autowired
    CommentService commentService;
       /**
     * 查询所有评论
     * @param comment 评论对象
     * @param pageSize 每页数量
     * @return 分页对象
     */
    @RequestMapping("/findAllComment")
    public Page<Comment> findAllComment(@RequestBody Comment comment, @RequestParam Integer pageNum, @RequestParam Integer pageSize) {
        return  commentService.findAllComment(comment, pageNum, pageSize);
    }
//    添加
    @PostMapping("/addComment")
    public void addComment(@RequestBody Comment comment){
        commentService.saveOrUpdate(comment);
    }

//    回复
    @PostMapping("/updatehuif")
    public void updatehuif(@RequestBody Comment comment){
        commentService.updatehuif(comment);
    }

    //    上传图片
    @PostMapping("/fileUpload")
    public JsonRes fileUpload(MultipartFile file) {
        AliyunOSSClientUtil aliyunOSSClientUtil = new AliyunOSSClientUtil();
        try {
            String s = aliyunOSSClientUtil.uploadImg2Oss(file);
            String imgUrl = aliyunOSSClientUtil.getImgUrl(s);
            return JsonRes.success(imgUrl);
        } catch (Exception e) {
            e.printStackTrace();
            return JsonRes.error("上传失败");
        }
    }
//    删除
    @PostMapping("/del")
    public void del(@RequestParam Integer id){
        commentService.removeById(id);
    }

}
